package com.erp.module.jiehao

import android.app.Activity
import androidx.appcompat.app.AlertDialog
import android.content.Intent
import android.view.View
import android.widget.AdapterView
import com.erp.Myapp
import com.erp.R
import com.erp.bean.JieHaoBean
import com.erp.bean.JieHaoKBean
import com.erp.mclass.QuanXian
import com.erp.module.jiehao.scJieHao.ScJieHaoKActivity
import com.erp.myAdapter.JieHaoKAdapter
import com.erp.myFragment.BaseKFragment
import com.erp.util.Myutil
import com.erp.view.PullToRefreshView
import kotlinx.android.synthetic.main.fragment_jiehao.*
import java.util.*

/**
 * Created by Administrator on 2017/11/16/016.
 * 作者：刘洋
 * 节号分配
 */
class FragmentKJieHao : BaseKFragment(), JieHaoContract.JieHaoView, View.OnClickListener, PullToRefreshView.OnHeaderRefreshListener, PullToRefreshView.OnFooterRefreshListener, AdapterView.OnItemLongClickListener {


    override fun PutTime() {
        Myapp.editor.putLong(FENPEITIME, Date().time / 1000)
        Myapp.editor.commit()
    }

    companion object {
        val FENPEITIME="JIEHAOTIME"
    }
    lateinit var P : IJieHaoP
    lateinit var adapter : JieHaoKAdapter
    lateinit var jiehaolist :MutableList<JieHaoKBean>
    lateinit var list :MutableList<JieHaoBean>
    var updialog: AlertDialog ?= null
    private val f = "FragmentKJieHao"
    override fun getLayoutId(): Int {
        fragmentname=f
        return R.layout.fragment_jiehao
    }

    override fun initData() {
        jiehaolist=ArrayList()
        list=ArrayList()
        P= IJieHaoP(1,jiehaolist,this, activity!!)
        adapter= JieHaoKAdapter(jiehaolist, activity!!, P, "isfenpeisuoyoujiehao", "isquxiaofenpeijiehao")
        JiJianLv.adapter=adapter
        if(QuanXian.isdeletefenpeijiehao) {
            JiJianLv.onItemLongClickListener = this
        }
        JiJianFragmentPv.setheader(true)
        JiJianFragmentPv.setOnHeaderRefreshListener(this)
        JiJianFragmentPv.setOnFooterRefreshListener(this)
        ScanLl.setOnClickListener(this)
        SouSuoIv.setOnClickListener(this)
        P.gainData("JieHao")
        JiJianFragmentPv.headerRefreshing()
        SouSuoIv.visibility=View.GONE
    }
    fun qingkong(){
        if(isFirstLoad){
            return
        }
        JiJianFragmentPv.headerRefreshing()
    }
    override fun fenPeiJieHaoOk(data: String) {
        P.xiala()
        if(updialog!=null) {
            updialog?.dismiss()
        }
    }

    override fun fenPeiJieHaoNo(erroe: String) {
        updialog?.dismiss()
        Myutil.MyTost(Myutil.decodeUnicode(erroe))
    }
    override fun onClick(v: View?) {
        when(v){
            ScanLl -> {
                if(QuanXian.isfenpeijiehao) {
                    val intent = Intent(activity, ScJieHaoKActivity::class.java)
                    intent.putExtra("code",1)
                    startActivityForResult(intent, 1)
                }else{
                    Myutil.MyTost("你没有上传节号的权限")
                }
            }
            SouSuoIv -> {
                updialog = Myutil.showupdatedialog(activity, "正在搜索节号数据", false)
                P.sousuo("")
                Myutil.notjianpan(activity,SouSuoIv)
            }
        }
    }
    override fun fenpeijiehao() {
        updialog = Myutil.showupdatedialog(activity, "正在访问服务器", false)
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(data==null || resultCode!= Activity.RESULT_OK){
            return
        }
        when(requestCode){
            1 -> {
                if(data.getBooleanExtra("isadd",false)){
                    P.xiala()
                }
            }
        }
    }

    override fun xialaOk() {
        updialog?.dismiss()
        JiJianFragmentPv?.onHeaderRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun xialaNo() {
        updialog?.dismiss()
        JiJianFragmentPv?.onHeaderRefreshComplete()
    }

    override fun shanglaOk() {
        updialog?.dismiss()
        JiJianFragmentPv?.onFooterRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun shanglaNo() {
        updialog?.dismiss()
        JiJianFragmentPv?.onFooterRefreshComplete()
    }
    override fun gainDataOk() {
        adapter.notifyDataSetChanged()
    }

    override fun onFooterRefresh(view: PullToRefreshView) {
        P.shangla()
    }
    override fun onHeaderRefresh(view: PullToRefreshView) {
        P.xiala()
    }
    override fun deleteOk(postion: Int) {
        jiehaolist.removeAt(postion)
        adapter.notifyDataSetChanged()
        updialog?.dismiss()
    }

    override fun deleteNo(error: String) {
        updialog?.dismiss()
    }

    override fun onItemLongClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long): Boolean {
        when(parent){
            JiJianLv -> {
                if(jiehaolist[position].is_distributed == "0"){
                    var c = context
                    if(c != null){
                        val dialog = AlertDialog.Builder(c)
                        dialog.setTitle("提示")
                        dialog.setMessage("确定要删除这个节号吗？")
                        dialog.setPositiveButton("确定") { dialog, which ->
                            updialog = Myutil.showupdatedialog(activity, "正在删除节号", false)
                            P.deleteJieHao(jiehaolist[position].batch_names,position)
                        }
                        dialog.setNegativeButton("取消") { dialog, which -> dialog.dismiss() }
                        dialog.show()
                    }

                }
            }
        }
        return true
    }
}